package com.team.ai.service;

import com.team.ai.entity.Community;
import com.team.ai.entity.Concern;
import com.team.ai.entity.Like;
import com.team.ai.entity.ResultEntity;
import com.team.ai.mapper.CommunityMapper;
import com.team.ai.mapper.ConcernMapper;
import com.team.ai.mapper.LikeMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class ConcernService {
    @Resource
    ConcernMapper concernMapper;
    @Resource
    CommunityMapper communityMapper;
    @Resource
    LikeMapper likeMapper;

    public String insert(int concernId,int befollowedId){
        try{
            return ResultEntity.success(concernMapper.insert(concernId,befollowedId)) ;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }
    public String delete(int concernId,int befollowedId){
        try{
            return ResultEntity.success(concernMapper.delete(concernId,befollowedId)) ;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }

    public String selectList(int concernId){
        try{
            List<Concern> concern = concernMapper.selectList(concernId);
            List<Community> list = new ArrayList<>();
            HashSet<Integer> like = new HashSet<>(); //被关注文章的id
            List<Like> likes=likeMapper.selectList(concernId);
            for (Like like1 : likes) {
                like.add(like1.getLikeid());
            }
//            Community community2 = communityMapper.selectById(id);
//            community2.setIslike(islike);
//            community2.setId(id);
            for(Concern o : concern){
                List<Community> community= communityMapper.selectByUserIds(o.getBefollowedid());
                for (Community community1 : community) {
                    community1.setIsgz(1);
                    if (like.contains(community1.getId())){
                        community1.setIslike(1);
                    }
                    //community1.setIslike(1);
                }
                list.addAll(community);
            }
//            for(Concern o : concern){
//                List<Community> community= communityMapper.selectByUserIds(o.getBefollowedid());
//                for (Community community1 : community) {
//                    community1.setIslike(1);
//                }
//                list.addAll(community);
//            }
            return ResultEntity.success(list) ;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }

    public String selectBeFolloWed(int befollowedId){
        try{
            return ResultEntity.success(concernMapper.selectBeFolloWed(befollowedId));
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }


}
